/*!
 * \file frtds_get_datetimes.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>
#include "frcomm.h"
#include "frmem.h"

#include "frbyte_buffer.h"
#include "frhex_dump.h"
#include "frlist.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frreport_comm.h"
#include "frct_comm.h"
#include "frtds_convert.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}


static void __case1(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    unsigned int ui_scale = 1;
    int ui_zone_len = 0;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_whole_flg = FR_YES;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x06\x01\x02\x03\x04\x05\x06", 7);

    i_ret = frtds_get_datetimes(p_st_data, ui_scale, ui_zone_len, i_endian, i_whole_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value,"1900-01-0100:00:00.0");

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;
}

static void __case2(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    unsigned int ui_scale = 2;
    int ui_zone_len = 0;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_whole_flg = FR_YES;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x06\x01\x02\x03\x04\x05\x06", 7);

    i_ret = frtds_get_datetimes(p_st_data, ui_scale, ui_zone_len, i_endian, i_whole_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value,"1900-01-0100:00:00.00");

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;
}

static void __case3(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    unsigned int ui_scale = 3;
    int ui_zone_len = 0;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_whole_flg = FR_YES;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x06\x01\x02\x03\x04\x05\x06", 7);

    i_ret = frtds_get_datetimes(p_st_data, ui_scale, ui_zone_len, i_endian, i_whole_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;
}

static void __case4(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    unsigned int ui_scale = 1;
    int ui_zone_len = 0;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_whole_flg = FR_NO;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x06\x01\x02\x03\x04\x05", 6);

    i_ret = frtds_get_datetimes(p_st_data, ui_scale, ui_zone_len, i_endian, i_whole_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_TODO);

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;
}


static void __case5(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    unsigned int ui_scale = 3;
    int ui_zone_len = 0;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_whole_flg = FR_YES;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x07\x01\x02\x03\x04\x05\x06\x07", 8);

    i_ret = frtds_get_datetimes(p_st_data, ui_scale, ui_zone_len, i_endian, i_whole_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value,"1900-01-0100:00:00.000");

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;
}

static void __case6(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    unsigned int ui_scale = 4;
    int ui_zone_len = 0;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_whole_flg = FR_YES;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x07\x01\x02\x03\x04\x05\x06\x07", 8);

    i_ret = frtds_get_datetimes(p_st_data, ui_scale, ui_zone_len, i_endian, i_whole_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value,"1900-01-0100:00:00.0000");

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;
}

static void __case7(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    unsigned int ui_scale = 5;
    int ui_zone_len = 0;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_whole_flg = FR_YES;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x07\x01\x02\x03\x04\x05\x06\x07", 8);

    i_ret = frtds_get_datetimes(p_st_data, ui_scale, ui_zone_len, i_endian, i_whole_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;
}

static void __case8(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    unsigned int ui_scale = 3;
    int ui_zone_len = 0;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_whole_flg = FR_NO;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x07\x01\x02\x03\x04\x05\x06", 7);

    i_ret = frtds_get_datetimes(p_st_data, ui_scale, ui_zone_len, i_endian, i_whole_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_TODO);

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;
}

static void __case9(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    unsigned int ui_scale = 5;
    int ui_zone_len = 0;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_whole_flg = FR_YES;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x08\x01\x02\x03\x04\x05\x06\x07\x08", 9);

    i_ret = frtds_get_datetimes(p_st_data, ui_scale, ui_zone_len, i_endian, i_whole_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value,"1900-01-0100:00:00.00000");

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;
}

static void __case10(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    unsigned int ui_scale = 6;
    int ui_zone_len = 0;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_whole_flg = FR_YES;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x08\x01\x02\x03\x04\x05\x06\x07\x08", 9);

    i_ret = frtds_get_datetimes(p_st_data, ui_scale, ui_zone_len, i_endian, i_whole_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value,"1900-01-0100:00:00.000000");

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;
}

static void __case11(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    unsigned int ui_scale = 7;
    int ui_zone_len = 0;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_whole_flg = FR_YES;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x08\x01\x02\x03\x04\x05\x06\x07\x08", 9);

    i_ret = frtds_get_datetimes(p_st_data, ui_scale, ui_zone_len, i_endian, i_whole_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value,"1900-01-0100:00:00.0000000");

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;
}

static void __case12(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    unsigned int ui_scale = 8;
    int ui_zone_len = 0;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_whole_flg = FR_YES;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x08\x01\x02\x03\x04\x05\x06\x07\x08", 9);

    i_ret = frtds_get_datetimes(p_st_data, ui_scale, ui_zone_len, i_endian, i_whole_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;
}

static void __case13(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    unsigned int ui_scale = 3;
    int ui_zone_len = 0;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_whole_flg = FR_NO;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x08\x01\x02\x03\x04\x05\x06\x07", 8);

    i_ret = frtds_get_datetimes(p_st_data, ui_scale, ui_zone_len, i_endian, i_whole_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_TODO);

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;
}

static void __case14(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    unsigned int ui_scale = 1;
    int ui_zone_len = 2;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_whole_flg = FR_YES;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x08\x01\x02\x03\x04\x05\x06\x00\x01", 9);

    i_ret = frtds_get_datetimes(p_st_data, ui_scale, ui_zone_len, i_endian, i_whole_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value,"1900-01-0100:00:00.+00:01");

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;
}

int add_suite_frtds_get_datetimes(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frtds_convert frtds_get_datetimes()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);
    CU_add_test(pSuite, "case6", __case6);
    CU_add_test(pSuite, "case7", __case7);
    CU_add_test(pSuite, "case8", __case8);
    CU_add_test(pSuite, "case9", __case9);
    CU_add_test(pSuite, "case10", __case10);
    CU_add_test(pSuite, "case11", __case11);
    CU_add_test(pSuite, "case12", __case12);
    CU_add_test(pSuite, "case13", __case13);
    CU_add_test(pSuite, "case14", __case14);
    
    return FR_SUCC;
}
